package Calendar.table.util;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import Calendar.table.day.DayPanel;


public class DayProvider {
	
	public static String[] dd = {"白班", "白班", "大副班", "小副班", "后夜", "后夜", "大休", "头夜", "头夜", "小休"};
	
	private ChineseCalendar[] acc;
	private ChineseCalendar today;
	private List<Duty> dList;
	
	public DayProvider() {
		this.setToday(new ChineseCalendar());
		dList = new ArrayList<Duty>();
		for (int i = 0; i < dd.length; i++) {
			dList.add(new Duty(new Color(i * 5, i * 5, i * 5), dd[i]));
		}
	}
	
	public DayPanel[][] getMonthData(){
		return getMonthData(today.get(Calendar.YEAR), today.get(Calendar.MONTH));
	}
	
	public DayPanel[][] getMonthData(int y, int m){
		int totalDaysOfMonth = (new ChineseCalendar(y, m, 1)).getActualMaximum(Calendar.DATE);
		this.acc = new ChineseCalendar[totalDaysOfMonth];
		for (int i = 0; i < acc.length; i++) {
			acc[i] = new ChineseCalendar(y, m, i + 1);
		}
		int firstDayOfWeek = acc[0].get(Calendar.DAY_OF_WEEK);
		
		
		
		DayPanel[][] DayPanel2D = new DayPanel[6][7];
		DayPanel[] DayPanel1D = new DayPanel[7];
		
		int weekIndex = 0;
		for (int i = firstDayOfWeek - 1; i < totalDaysOfMonth + firstDayOfWeek - 1; i++) {
			DayPanel1D[i % 7] = new DayPanel(acc[i - firstDayOfWeek + 1], dList.get(Math.abs((i - 6)% 10)), today.toString());
			if (i % 7 == 6) {
				DayPanel2D[weekIndex] = DayPanel1D;
				DayPanel1D = new DayPanel[7];
				weekIndex++;
			}
		}
		DayPanel2D[weekIndex] = DayPanel1D;
		return DayPanel2D;
	}
	
	public ChineseCalendar getChineseCalendar(int y, int m){
		return new ChineseCalendar(y, m, 1);
	}

	public ChineseCalendar getToday() {
		return today;
	}

	public void setToday(ChineseCalendar today) {
		this.today = today;
	}

}
